package net.mysoft.budgetsoft.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import net.mysoft.budgetsoft.system.dao.MenuDao;
import net.mysoft.budgetsoft.system.dao.UserDao;
import net.mysoft.budgetsoft.system.domain.TMenu;
import net.mysoft.budgetsoft.system.domain.TRole;
import net.mysoft.budgetsoft.system.domain.TUser;
import net.mysoft.budgetsoft.system.service.MainService;
import net.mysoft.framework.util.DateCompartor;
import net.mysoft.framework.util.Maps;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;

@SuppressWarnings({"rawtypes", "unchecked"})
@Service("mainService")
public class MainServiceImpl implements MainService{
	
	@Autowired
	private MenuDao menuDao;
	@Autowired
	private UserDao userDao;

	@Override
	public List<TMenu> getRootMenus() {
		List<TMenu> roots = menuDao.find("from TMenu where parent is null order by orderby");
		for(TMenu menu:roots) {
			//System.err.println(menu.getName());
			List<TMenu> menus = menu.getMenus();
			List tree = new ArrayList();
			for(TMenu t:menus) {
				//{"attributes":{"target":"CXFDemo","url":"/ws"},"checked":false,"iconCls":"ext-icon-world","id":"cxfDemo","pid":"demo","state":"open","text":"ApacheCXF示例"},
				//[{"attribute":{"url":"http://www.baidu.com"},"id":"4028802846d7271a0146d7271fbb0000","pid":"4028802846d6d6fc0146d6d7007f0000","text":"系统定制"},{"attribute":{"url":"http://www.baidu.com"},"id":"4028802846d728ef0146d728f3c80000","pid":"4028802846d7271a0146d7271fbb0000","text":"参数定制"},{"attribute":{"url":"http://www.baidu.com"},"id":"4028802846d728ef0146d728f3d80001","pid":"4028802846d7271a0146d7271fbb0000","text":"模块定制"}]
				Map map = new HashMap();
				map.put("id", t.getId());
				map.put("text", t.getName());
				TMenu parent = t.getParent();
				map.put("pid", parent==null?null:parent.getId());
				Map attr = new HashMap();
				attr.put("url", t.getUrl());
				map.put("attributes", attr);
				tree.add(map);
			}
			//System.err.println(JSON.toJSONString(tree));
			menu.setRemark(JSON.toJSONString(tree));
		}
		return roots;
	}

	@Override
	public Set<TMenu> getUserRootMenus(String userId) {
		TUser user = userDao.load(TUser.class, userId);
		List<TRole> roles = user.getRoles();
		Set<TMenu> rootMenus = new TreeSet<TMenu>(new DateCompartor());
		Map<String, Set<TMenu>> menuMap = new HashMap<String, Set<TMenu>>();
		for(TRole role:roles) {
			List<TMenu> menus = role.getMenus();
			for(TMenu menu:menus) {
				recursionMenu(rootMenus, menuMap, menu);
			}
		}
		
		for(TMenu menu:rootMenus) {
			//System.out.println(menu.getName());
			Set<TMenu> menus = menuMap.get(menu.getId());
			List tree = new ArrayList();
			for(TMenu m:menus) {
				Map map = Maps.newHashMap("id", m.getId(), "text", m.getName(), 
						"pid", m.getParent().getId(), "attributes", Maps.newHashMap("url", m.getUrl()));
				tree.add(map);
			}
			menu.setRemark(JSON.toJSONString(tree));
			/*List<TMenu> menus = menu.getMenus();
			List trees = new ArrayList();
			for(TMenu t:menus) {
				//{"attributes":{"target":"CXFDemo","url":"/ws"},"checked":false,"iconCls":"ext-icon-world","id":"cxfDemo","pid":"demo","state":"open","text":"ApacheCXF示例"},
				//[{"attribute":{"url":"http://www.baidu.com"},"id":"4028802846d7271a0146d7271fbb0000","pid":"4028802846d6d6fc0146d6d7007f0000","text":"系统定制"},{"attribute":{"url":"http://www.baidu.com"},"id":"4028802846d728ef0146d728f3c80000","pid":"4028802846d7271a0146d7271fbb0000","text":"参数定制"},{"attribute":{"url":"http://www.baidu.com"},"id":"4028802846d728ef0146d728f3d80001","pid":"4028802846d7271a0146d7271fbb0000","text":"模块定制"}]
				if(sonMenuIds.contains(t.getId())) {
					Map map = new HashMap();
					map.put("id", t.getId());
					map.put("text", t.getName());
					TMenu parent = t.getParent();
					map.put("pid", parent==null?null:parent.getId());
					Map attr = new HashMap();
					attr.put("url", t.getUrl());
					map.put("attributes", attr);
					trees.add(map);
				}
			}
			//System.err.println(JSON.toJSONString(tree));
			menu.setRemark(JSON.toJSONString(trees));*/
		}
		return rootMenus;
	}
	
	private void recursionMenu(Set<TMenu> roots, Map<String, Set<TMenu>> menuMap, TMenu menu) {
		if(menu.getParent()!=null) {
			String rootId = menu.getRoot().getId();
			Set<TMenu> menuSet = menuMap.get(rootId);
			if(menuSet==null) menuMap.put(rootId, new HashSet<TMenu>());
			menuMap.get(rootId).add(menu);
			recursionMenu(roots, menuMap, menu.getParent());
		} else {
			roots.add(menu);
		}
	}
	
}
